package com.example.equipmentjava.xiaoxinyan;

/**
 * @author 邓昕劼
 * @version 1.3
 */


import com.example.equipmentjava.xiaoxinyan.pojo.PushCallLog;
import com.example.equipmentjava.xiaoxinyan.pojo.PushCallThePolice;
import com.example.equipmentjava.xiaoxinyan.pojo.PushStatusNotification;
import com.example.equipmentjava.xiaoxinyan.pojo.ResponseDTO;
import com.example.equipmentjava.xiaoxinyan.utils.URLDecoderUtil;
import com.example.equipmentjava.xiaoxinyan.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 孝心眼 同步到启效云
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/FilialEyes")
public class TimedYController {

    public static final Logger logger = LoggerFactory.getLogger(TimedYController.class);


    /**
     * 用户删除
     * @param
     * @return
     */
    @PostMapping("DeleteUser")
    public ResponseDTO DeleteUser(String name,String sn, String UUID, String userid){
        try {
            logger.info("孝心眼--删除用户--用户姓名："+URLDecoderUtil.decode(name)+" 社区标识："+UUID+" 设备号："+sn+" 用户标识："+userid);
            String s = Utils.DeleteUser(URLDecoderUtil.decode(name), sn, UUID, userid);
            logger.info("孝心眼--删除用户--外部接口响应结果：" + s);
            net.sf.json.JSONObject AlarmJson = net.sf.json.JSONObject.fromObject(s);
            ResponseDTO responseDTO = new ResponseDTO();
            responseDTO.setSuccess(Boolean.valueOf(AlarmJson.getString("Success")));
            return responseDTO;
        } catch (Exception je) {
            je.printStackTrace();
            return null;
        }
    }



    /**
     * 用户修改
     * @param
     * @return
     */
    @PostMapping("UpdateUser")
    public ResponseDTO UpdateUser(String name, String gender, String birthday, String telephone, String sn, String address, String UUID,String userid){
        try {
            logger.info("孝心眼--修改用户--用户姓名："+URLDecoderUtil.decode(name)+" 性别："+gender+" 生日："+birthday+" 手机号："+telephone+" 设备号："+sn+" 用户地址："+URLDecoderUtil.decode(address)+" 社区标识："+UUID+" 用户标识："+userid);
            String s = Utils.UpdateUser(URLDecoderUtil.decode(name), gender, birthday, telephone, sn, URLDecoderUtil.decode(address), UUID, userid);
            logger.info("孝心眼--修改用户--外部接口响应结果：" + s);
            net.sf.json.JSONObject AlarmJson = net.sf.json.JSONObject.fromObject(s);
            ResponseDTO responseDTO = new ResponseDTO();
            responseDTO.setSuccess(Boolean.valueOf(AlarmJson.getString("Success")));
            return responseDTO;
        } catch (Exception je) {
            je.printStackTrace();
            return null;
        }
    }



    /**
     * 用户新增
     * @param
     * @return
     */
    @PostMapping("CreateUser")
    public ResponseDTO CreateUser(String name, String gender, String birthday, String telephone, String sn, String address, String UUID){
        try {
            logger.info("孝心眼--新增用户--用户姓名："+URLDecoderUtil.decode(name)+" 性别："+gender+" 生日："+birthday+" 手机号："+telephone+" 设备号："+sn+" 用户地址："+URLDecoderUtil.decode(address)+" 社区标识："+UUID);
            String s = Utils.InsertUser(URLDecoderUtil.decode(name), gender, birthday, telephone, sn, URLDecoderUtil.decode(address), UUID);
            net.sf.json.JSONObject AlarmJson = net.sf.json.JSONObject.fromObject(s);
            ResponseDTO responseDTO = new ResponseDTO();
            responseDTO.setSuccess(Boolean.valueOf(AlarmJson.getString("Success")));
            responseDTO.setUUID(AlarmJson.getString("Content"));
            logger.info("孝心眼--新增用户--外部接口响应结果：" + s);
            return responseDTO;
        } catch (Exception je) {
            je.printStackTrace();
            return null;
        }
    }




    /**
     * 设备状态的实时通知接口，触发事件
     * 设备上线
     * 设备下线
     * 状态变更
     * @param body
     * @return
     */
    @PostMapping("PushStatusNotification")
    @ResponseBody
    public ResponseEntity<String> PushStatusNotification(@RequestBody String body){
        logger.info("孝心眼--设备状态的实时通知接口--body数据："+body);
        net.sf.json.JSONObject Json = net.sf.json.JSONObject.fromObject(body);
        PushStatusNotification pushStatusNotification = Utils.PushStatusNotification(Json);  //提取body中的json数据并同步启效云
        logger.info("孝心眼--设备状态的实时通知接口--处理之后的数据：" + pushStatusNotification);
        return ResponseEntity.ok("200");
    }



    /**
     * 推送所有，包括运营商线路和网络电话的通话日志
     * @param body
     * @return
     */
    @PostMapping("PushCallLog")
    @ResponseBody
    public ResponseEntity<String> PushCallLog(@RequestBody String body){
        logger.info("孝心眼--通话日志--body数据："+body);
        net.sf.json.JSONObject Json = net.sf.json.JSONObject.fromObject(body);
        PushCallLog pushCallLog = Utils.PushCallLog(Json);  //提取body中的json数据并同步启效云
        logger.info("孝心眼--通话日志--处理之后的数据：" + pushCallLog);
        return ResponseEntity.ok("200");
    }



    /**
     * 报警事件通知接口
     * @param body
     * @return
     */
    @PostMapping("PushCallThePolice")
    @ResponseBody
    public ResponseEntity<String> PushCallThePolice(@RequestBody String body){
        logger.info("孝心眼--报警事件--body数据："+body);
        net.sf.json.JSONObject Json = net.sf.json.JSONObject.fromObject(body);
        PushCallThePolice pushCallThePolice = Utils.PushCallThePolice(Json);  //提取body中的json数据并同步启效云
        logger.info("孝心眼--报警事件--处理之后的数据：" + pushCallThePolice);
        return ResponseEntity.ok("200");
    }













}
